var util = require('../../utils/util.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShow: false,
    hangye: {
      data: [{
          domainId: 1,
          name: '医疗'
        },
        {
          domainId: 2,
          name: '医药'
        }
      ],
      index: 0
    },
    company: [],
    job: [],
    city: [],
    domains: [],
    dataArr: [],
    placeHolder: '',
    dataLabel: '',
    parm: {
      name: '',
      phone: '',
      companyId: '',
      jobId: '',
      cityId: '',
      workAge: '',
      bestDomain: '',
      domainId: '',
      domainName: '',
      expectJob: '',
      expectCompany1: '',
      expectCompany2: '',
      expectCompany3: ''
    },
    phoneReg: false,
    status:'add'
    //   String unionid;
    // 姓名 String name;
    // 手机号 String phone;
    // 公司编号 Integer companyId; 
    // 职位 Integer jobId;
    // 城市 Integer cityId;
    // 工作年限 Integer workAge;
    // 管理区域 String bestDomain;
    // 期望细分领域 Integer domainId;
    // 期望细分领域name String domainName;
    // 期望职位id Integer expectJob;
    // 期望公司1id Integer expectCompany1;
    // 期望公司2id Integer expectCompany2;
    // 期望公司3id Integer expectCompany3; 
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.getCompanys()
    this.getJobs()
    this.getCitys()
    this.getByUnionid();
  },
  getCompanys() {
    var that = this;
    return new Promise((resolve, reject) => {
      util.post(app.api.getSchCompanys, {
        name: ''
      }, function (res) {
        if (res.code === "200") {
          that.setData({
            company: res.data
          }, () => {
            console.log(that.data.company)
            resolve()
          })
        }

      })
    })
  },

  getJobs() {
    var that = this;
    util.post(app.api.getJobs, {
      name: ''
    }, function (res) {
      that.setData({
        job: res.data
      })
    })
  },

  getCitys() {
    var that = this;
    util.post(app.api.getCitys, {
      name: ''
    }, function (res) {
      that.setData({
        city: res.data
      })
    })
  },

  getDomains() {
    return new Promise((resolve, reject) => {
      var that = this;
      let hangye = that.data.hangye
      util.post(app.api.getDomains, {
        name: '',
        domaiParentId: hangye.data[hangye.index].domainId
      }, function (res) {
        that.setData({
          domains: res.data
        }, () => {
          resolve()
        })
      })
    })
  },
  getByUnionid() {
    util.post(app.api.getByUnionid, {
      unionid: wx.getStorageSync('unionid')
    },  (res)=> {
      if(res.data){
        console.log(res.data)

        let data=res.data;
        this.setData({
          status:res.data.phone&&'see',
          parm:{
            name: data.name,
            phone: data.phone,
            companyId: {'id':data.companyId,'name':data.companyName},
            jobId: {'id':data.jobId,'name':data.jobName},
            cityId: {'id':data.companyId,'name':data.cityName},
            workAge: data.workAge,
            bestDomain: data.bestDomain,
            domainId: '',
            domainName: {'id':data.domainId,'name':data.domainName},
            expectJob: {'id':data.expectJob,'name':data.expectJobName},
            expectCompany1: {'id':data.expectCompany1,'name':data.expectCompanyName1},
            expectCompany2: {'id':data.expectCompany2,'name':data.expectCompanyName2},
            expectCompany3: {'id':data.expectCompany3,'name':data.expectCompanyName3}
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  bindPickerChange(e) {
    let data = this.data[e.target.dataset.name]
    console.log(e);
    console.log(data);
    data['index'] = e.detail.value
    console.log(data);
    this.setData({
      ...this.data,
      ...data,
      parm:{
        ...this.data.parm,
        "domainName":'',
        "companyId":'',
        "expectCompany1":'',
        "expectCompany2":'',
        "expectCompany3":''
      }
    })

  },
  selectData: function (e) {
    var label = (e.currentTarget.dataset.label)
    var data = {}
    let promise = new Promise((resolve, rejct) => {
      if (label === 'domainName') {
        if (Number(this.data.hangye.index) === -1) {
          wx.showToast({
            title: '请先选择所在行业',
            icon: 'none'
          })
          return false
        }
        this.getDomains().then(() => {
          resolve()
        })
      } else if (label === 'companyId' ||
        label === 'expectCompany1' ||
        label === 'expectCompany2' ||
        label === 'expectCompany3') {
        if (Number(this.data.hangye.index) === -1) {
          wx.showToast({
            title: '请先选择所在行业',
            icon: 'none'
          })
          return false
        }
        this.getCompanys().then((e) => {
          resolve()
        })
      } else {
        resolve()
      }
    })
    promise.then(() => {
      this.setData({
        isShow: true,
        dataArr: (() => {
          if (label === 'jobId' || label === 'expectJob') {
            return this.data.job
          } else if (label === 'cityId') {
            return this.data.city
          } else if (label === 'domainName') {
            return this.data.domains
          } else {
            return this.data.company
          }
        })(),
        placeHolder: e.target.dataset.pl,
        dataLabel: e.target.dataset.label
      }, () => {
        console.log(this.data)
      })
    })
  },
  hiddenCompany: function (e) {
    this.setData({
      isShow: e.detail
    })
  },
  setSelectValue(e) {
    let parm = this.data.parm
    parm[e.detail.label] = e.detail.value
    this.setData({
      ...this.data,
      ...parm,
      dataArr: []
    }, () => {
      console.log(this.data)
    })
  },
  inputeidt(e) {
    let dataset = e.currentTarget.dataset
    this.data.parm[dataset.item] = e.detail.value
  },
  bindblur(e) {
    var that=this;
    if (/^1\d{10}$/.test(e.detail.value)) {
      util.post(app.api.checkPhone, {
          phone: e.detail.value
        },
        function (res) {
          if (Number(res.code) === 400) {
            wx.showToast({
              title: res.msg,
              icon: 'none'
            })

            that.setData({
              phoneReg: false
            })
          } else {

            that.setData({
              phoneReg: true
            })
          }
        })
    }
  },
  save() {
    if(this.data.status==='see'){
      
      this.setData({
        status:'editor'
      })
      
      return false
    }
    let parm = this.data.parm
    let err = ''
    if (!parm.name) {
      err = '请输入姓名'
    } else if (!/^1\d{10}$/.test(parm.phone)) {
      err = '请输入正确的手机号'
    } else if (Number(this.data.hangye.index) === -1) {
      err = '请选择所在行业'
    } else if (!parm.companyId.id) {
      err = '请选择现任公司'
    } else if (!parm.jobId.id) {
      err = '请选择现任职位'
    } else if (!parm.cityId.id) {
      err = '请选择所在城市'
    } else if (!parm.workAge) {
      err = '请输入现任工作年限'
    } else if (!parm.bestDomain) {
      err = '请输入管理区域'
    } else if (!parm.domainName.id) {
      err = '请选择期望细分领域'
    } else if (!parm.expectCompany1) {
      err = '请选择第一期望公司'
    }
    if (err) {
      wx.showToast({
        title: err,
        icon: 'none'
      })
      return false
    } else {
      if(this.data.phoneReg===false&&this.data.status==='add'){
        wx.showToast({
          'title':'手机号已注册，请更换手机号',
          'icon':'none'
        })
        return false
      }
      let data = {
        unionid: wx.getStorageSync('unionid'),
        ...parm,
        companyId: parm.companyId.id,
        jobId: parm.jobId.id,
        cityId: parm.cityId.id,
        domainId: parm.domainName.id,
        domainName: parm.domainName.name,
        expectJob: parm.expectJob.id,
        expectCompany1: parm.expectCompany1.id,
        expectCompany2: parm.expectCompany2.id == 'undefined' ? 0 : parm.expectCompany2.id,
        expectCompany3: parm.expectCompany3.id == 'undefined' ? 0 : parm.expectCompany3.id
      }
      util.post(app.api.userSave, data, function (res1) {
        if (res1.code == 200) {
          wx.showToast({
            title: '资料提交成功',
            icon: 'success'
          })
          wx.navigateBack({})
        }
      })
    }

  }
})